//
//  ZCAdressPickerView.m
//  zctx
//
//  Created by apple on 15/12/4.
//  Copyright © 2015年 sanliang. All rights reserved.
//

#import "ZCAdressPickerView.h"
@interface ZCAdressPickerView()

@property (strong, nonatomic) UIButton *cannelBtn;
@property (strong, nonatomic) UIButton *sureBtn;
@property (nonatomic,strong)UIControl *overlayView;

@property (nonatomic, strong) NSArray * provinces;  //整个省_城市_地区的plist数组列表

@property (nonatomic, strong) NSArray * cities; //城市列表

@property (nonatomic, strong) NSArray * areas; //地区列表

@property (nonatomic, assign) NSInteger secProvincesRow;  //选择省行

@property (nonatomic, assign) NSInteger secCitiesRow;  //选择省行

@property (nonatomic, assign) NSInteger secAreasRow;  //选择省行

@end

@implementation ZCAdressPickerView
-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        self.provinces = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"area.plist" ofType:nil]];
        self.cities = [[self.provinces objectAtIndex:0] objectForKey:@"cities"];
        self.areas = [[self.cities objectAtIndex:0] objectForKey:@"areas"];
        [self initView];
    }
    return self;
}

- (void)initView
{
    self.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.PickerView = [[UIPickerView alloc]init];
    [self addSubview:self.PickerView];
    self.PickerView.backgroundColor = [UIColor whiteColor];
    [self.PickerView makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.bottom).with.offset(0);
        make.left.equalTo(self.left).with.offset(0);
        make.right.equalTo(self.right).with.offset(0);
    }];
    self.PickerView.delegate = self;
    self.PickerView.dataSource = self;
    [self.PickerView selectRow:self.secProvincesRow inComponent:0 animated:YES];
    [self.PickerView selectRow:self.secCitiesRow inComponent:1 animated:YES];
    [self.PickerView selectRow:self.secAreasRow inComponent:2 animated:YES];
    [self addSubview:self.PickerView];
    
    self.cannelBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [self addSubview:self.cannelBtn];
    [self.cannelBtn makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.PickerView.top).with.offset(0);
        make.left.equalTo(self.left).with.offset(0);
        make.top.equalTo(self.top).with.offset(0);
        make.width.mas_equalTo(130);
    }];
    [self.cannelBtn setTitle:@"取消" forState:UIControlStateNormal];
    
    [self.cannelBtn addTarget:self action:@selector(removeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    self.sureBtn =[UIButton buttonWithType:UIButtonTypeSystem];
    [self addSubview:self.sureBtn];
    [self.sureBtn makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.PickerView.top).with.offset(0);
        make.right.equalTo(self.right).with.offset(0);
        make.top.equalTo(self.top).with.offset(0);
        make.width.mas_equalTo(130);
    }];
    [self.sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    [self.sureBtn addTarget:self action:@selector(sureBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    _overlayView = [[UIControl alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    _overlayView.backgroundColor = [UIColor colorWithRed:.16 green:.17 blue:.21 alpha:.5];
    [_overlayView addTarget:self
                     action:@selector(dismiss)
           forControlEvents:UIControlEventTouchUpInside];
    
}


// 取消
- (void)removeBtnClick:(UIButton *)sender {
    // 开始动画
    [self dismiss];
    NSLog(@"sdfsdf");
}

// 确定
- (void)sureBtnClick:(UIButton *)sender {
    
    NSString *str_province = [self pickerView:self.PickerView titleForRow:[self.PickerView selectedRowInComponent:0] forComponent:0];
    NSString *str_city = [self pickerView:self.PickerView titleForRow:[self.PickerView selectedRowInComponent:1] forComponent:1];
    NSString *str_area = [self pickerView:self.PickerView titleForRow:[self.PickerView selectedRowInComponent:2] forComponent:2];
    
    NSString *addressStr = [NSString stringWithFormat:@"%@%@%@",str_province,str_city,str_area];
    if (_delegate && [_delegate respondsToSelector:@selector(getAddressStrPicker:)]) {
        [_delegate getAddressStrPicker:addressStr];
    }
    [self dismiss];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (component == 0){
        //省
        NSString * str_provinces = [[self.provinces objectAtIndex:row] objectForKey:@"state"];
        return str_provinces;
    }else if (component == 1){
        //市
        NSString * str_cities = [[self.cities objectAtIndex:row] objectForKey:@"city"];
        return str_cities;
    }else{
        //区
        NSString * str_areas = [self.areas objectAtIndex:row];
        return str_areas;
    }
    
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    switch (component)
    {
        case 0:
        {
            self.cities = [[self.provinces objectAtIndex:row] objectForKey:@"cities"];
            self.areas = [[self.cities objectAtIndex:0] objectForKey:@"areas"];
            [pickerView reloadComponent:1];
            [pickerView reloadComponent:2];
        }
            break;
            
        case 1:
        {
            self.areas = [[self.cities objectAtIndex:row] objectForKey:@"areas"];
            [pickerView reloadComponent:2];
        }
            break;
        default:
            break;
    }
}


#pragma mark --- UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 3;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    switch (component)
    {
        case 0:
        {
            NSInteger proCount = self.provinces.count;
            return proCount > 0 ? proCount : 0;
        }
            break;
            
        case 1:
        {
            NSInteger cityCount = self.cities.count;
            
            return cityCount > 0 ? cityCount : 0;
        }
            break;
            
        case 2:
        {
            NSInteger areasCount = self.areas.count;
            return areasCount > 0 ? areasCount : 0;
        }
            break;
            
        default:
        {
            return 0;
        }
            break;
    }
}

#pragma mark - animations

- (void)fadeIn
{
    self.transform = CGAffineTransformMakeScale(1.2, 1.2);
    self.alpha = 0;
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 1;
        self.transform = CGAffineTransformMakeScale(1, 1);
    }];
    
}
- (void)fadeOut
{
    [UIView animateWithDuration:0.3 animations:^{
        self.transform = CGAffineTransformMakeScale(1.2, 1.2);
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        if (finished) {
            [_overlayView removeFromSuperview];
            [self removeFromSuperview];
        }
    }];
}


- (void)show
{
    UIWindow *keywindow = [[UIApplication sharedApplication] keyWindow];
    [keywindow addSubview:_overlayView];
    [keywindow addSubview:self];
    
    [self makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(keywindow.bottom).with.offset(0);
        make.left.equalTo(keywindow).with.offset(0);
        make.right.equalTo(keywindow).with.offset(0);
        make.height.mas_equalTo(self.bounds.size.height);
    }];
    [self fadeIn];
}

- (void)dismiss
{
    [self fadeOut];
}



@end
